
;original from Ramones (http://karoshi.auic.es/index.php?topic=628.0)
; -----------------------
; SEARCH_SLOTSET
; Posiciona en pagina 2
; Nuestro ROM.
; -----------------------

search_slotset:
		call search_slot
		jp ENASLT


; -----------------------
; SEARCH_SLOT
; Busca slot de nuestro rom
; -----------------------

search_slot:

	call RSLREG
	rrca
	rrca
	and 3
	ld c,a
	ld b,0
	ld hl,0FCC1h
	add hl,bc
	ld a,(hl)
	and 080h
	or c
	ld c,a
	inc hl
	inc hl
	inc hl
	inc hl
	ld a,(hl)
	and 0Ch
	or c
	ld h,080h
	ld (slotvar),a
	ret



;48KB routines

; ------------------------------
; SETROMPAGE0			
; Posiciona nuestro cartucho en 
; Pagina 0
; -----------------------------

setrompage0:			
				
        				ld		a,(slotvar)		
				        jr		setslotpage0	
				
			
			
; ------------------------------
; RECBIOS
; Posiciona la bios ROM
; -------------------------------
					
recbios:		
        				ld		a,(EXPTBL)
				
			

; ---------------------------
; SETSLOTPAGE0
; Posiciona el slot pasado 
; en pagina 0 del Z80
; A: Formato FxxxSSPP
; ----------------------------
            
setslotpage0:       
	di

	ld      b,a                 ; B = Slot param in FxxxSSPP format                


	in      a,(0A8h)
	and     011111100b
	ld      d,a                 ; D = Primary slot value

	ld      a,b         

	and     03  
	or      d
	ld      d,a                 ; D = Final Value for primary slot 


	out     (0A8h),a

	; Check if expanded
	ld      a,b
	bit     7,a
	ret     z   

	and     03h                             
	rrca
	rrca
	and     011000000b
	ld      c,a                 
	ld      a,d
	and     00111111b
	or      c
	ld      c,a                 ; Primary slot value with main slot in page 3  

	ld      a,b
	and     00001100b
	rrca
	rrca    
	and     03h
	ld      b,a                 ; B = Expanded slot in page 3
	ld      a,c
	out     (0A8h),a            ; Slot : Main Slot, xx, xx, Main slot
	ld      a,(0FFFFh)
	cpl
	and     011111100b
	or      b
	ld      (0FFFFh),a          ; Expanded slot selected 

	ld      c,a

								; Slot Final. Ram, rom c, rom c, Main
	ld      a,d                 ; A = Final value
	out     (0A8h),a

	and     3                   ; Set value in STLTBL 
	ld      de,SLTTBL    
	add     a,e
	ld      e,a
	jr      nc,.nocarry
	inc     d
.nocarry:
	ld      a,c
	ld      (de),a

	ret

                    
setslotpage0_end:


;original from Ramones END



; 64KB stuff

setup_page3_activation:
; current RAM status (page 3) .. TO RETURN
; 0xa8 por value, 0xfffff value     ... saved
	in		a,[0xa8]
	ld		b,a
	ld		a,[0xffff]
	cpl
	ld		c,a							;bc holds return stuff
	ld		[ram_cfg],bc

; change to ROM page 3
; check current on page 1
; replicate to a8 and to FFFFF
; 
; 
	ld		a,[slotvar]
[6]	sla		a
	ld		b,a
	ld		a,[slotvar]
	rlca							;if c set, subslots!
	;jr		nc,done
	and		0x18					;00011000
[3]	sla		a
	ld		c,a
;done:
	in		a,[0xa8]
	and		0x3f
	or		b
	ld		b,a						;ready slot!
	ld		a,[0xffff]
	cpl
	and		0x3f
	or		c

[2]	sra		c
	and		11001111b
	or		c
	
[2]	sra		c
	and		11110011b
	or		c
	
	
	ld		c,a						;ready subslot
	
	;ld		c,01010100b
	
	ld		[rom_cfg],bc

	ret



activate_rom_page3:
	di
	pop		hl
	exx
	ld		bc,[ram_cfg]			;to return later
	exx

	ld		bc,[rom_cfg]
	ld		a,b
	out		(0xa8),a				;primary slot
	
	ld		a,c
	ld		[0xffff],a				;secondary slot

	jp		(hl)





restore_ram_page3:
	exx
	ld		a,b
	out		(0xa8),a				;primary slot
	
	ld		a,c
	ld		[0xffff],a				;secondary slot

	ei

	ret

